import {isPromise} from '../utils/tool'
import {Application, AppStatus} from '../types'

export default function unmountApp(app: Application): Promise<any> {
	app.status = AppStatus.BEFORE_UNMOUNT

	let result = app.unmount?.(app.props) as any
	if (!isPromise(result)) {
		result = Promise.resolve(result)
	}

	return result
		.then(() => {
			app.status = AppStatus.UNMOUNTED
		})
		.catch((err: Error) => {
			throw err
		})
}
